﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataAccessLayer;

public partial class web_Controls_ctrl_vl_left_kdt_details : System.Web.UI.UserControl
{
    Common _common = null;
    public string _id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        _common = new Common();
		Response.Write("aaaa");
        if (Request.Params["id"] != null)
        {
            if (!Page.IsPostBack)
            {
                if (Session["so1"] == null)
                {
                    Random random = new Random();
                    Session["so1"] = random.Next(1, 9);
                    Session["so2"] = random.Next(1, 9);
                }
            }

            lbl_dk_ask.Text = Session["so1"].ToString() + " + " + Session["so2"].ToString() + " = ";
            _id = _common.TextFormat(Request.Params["id"].ToString());
            LoadKdtDetail(_id);
        }
    }
    void LoadKdtDetail(string id)
    {
        string _sql = "select A.*, B.ten_nganh_kd, C.ten_trinh_do_dt, D.ten_hinh_thuc_dt from daotao_khoa_dao_tao as A ";
            _sql += " left join DanhMuc_nganh_kinh_doanh as B on A.ma_nganh_dt = B.ma_nganh_kd ";
            _sql += " left join DaoTao_trinh_do_dao_tao as C on A.ma_trinh_do_dt = C.ma_trinh_do_dt ";
            _sql += " left join DaoTao_hinh_thuc_dao_tao as D on A.ma_hinh_thuc_dt = D.ma_hinh_thuc_dt where ma_khoa_dt = '" + id + "'";
        DataSet _ds = _common.ExeceuteDataset(_sql);
		if(_ds.Tables[0].Rows.Count>0){
        if (LoadCosodaotao(_ds.Tables[0].Rows[0]["ma_co_so_dt"].ToString()))
        {
            lbl_ghichu.Text = _ds.Tables[0].Rows[0]["chi_tiet_khac"].ToString();
            lbl_hinhthuc_dt.Text = _ds.Tables[0].Rows[0]["ten_hinh_thuc_dt"].ToString();
            lbl_hocphi.Text = _ds.Tables[0].Rows[0]["hoc_phi"].ToString() + " (đồng)";
            lbl_ma_kdt.Text = _ds.Tables[0].Rows[0]["ma_khoa_dt"].ToString();
            lbl_mota.Text = _ds.Tables[0].Rows[0]["mo_ta"].ToString();
            lbl_nganh_dt.Text = _ds.Tables[0].Rows[0]["ten_nganh_kd"].ToString();
            lbl_ngay_hethan.Text = _ds.Tables[0].Rows[0]["ngay_dong_ho_so"].ToString();
            lbl_ngay_khaigiang.Text = _ds.Tables[0].Rows[0]["ngay_khai_giang"].ToString();
            lbl_ngay_nhanhoso.Text = _ds.Tables[0].Rows[0]["ngay_nhan_ho_so"].ToString();
            lbl_soluong_dt.Text = _ds.Tables[0].Rows[0]["so_hoc_vien"].ToString() + " (học viên)";
            lbl_soluong_gt.Text = _ds.Tables[0].Rows[0]["so_duoc_gtvl"].ToString() + " (học viên)";
            lbl_ten_kdt.Text = _ds.Tables[0].Rows[0]["ten_khoa_dt"].ToString();
            lbl_thoigianhoc.Text = _ds.Tables[0].Rows[0]["thoi_gian_hoc"].ToString();
            lbl_trinhdo_dt.Text = _ds.Tables[0].Rows[0]["ten_trinh_do_dt"].ToString();
        }}
    }

    bool LoadCosodaotao(string ma_doanh_nghiep)
    {
        DataSet _ds_dn = _common.ExeceuteDataset("select * from DaoTao_co_so_dao_tao where ma_co_so_dt = '" + ma_doanh_nghiep + "' and kich_hoat = 1");
        if (_ds_dn.Tables[0].Rows.Count > 0)
        {
            if ((bool)_ds_dn.Tables[0].Rows[0]["kich_hoat"])
            {
                lbl_csdt_diachi.Text = _ds_dn.Tables[0].Rows[0]["dia_chi"].ToString() + " " + _common.execScalar("select dbo.fcGetDiaChiChiTiet('" + _ds_dn.Tables[0].Rows[0]["ma_dia_chi"].ToString() + "')");
                lbl_csdt_dienthoai.Text = _ds_dn.Tables[0].Rows[0]["dien_thoai"].ToString();
                lbl_csdt_ghichu.Text = _ds_dn.Tables[0].Rows[0]["chi_tiet_khac"].ToString();
                lbl_csdt_gioithieu.Text = _ds_dn.Tables[0].Rows[0]["gioi_thieu"].ToString();
                lbl_csdt_namthanhlap.Text = _ds_dn.Tables[0].Rows[0]["nam_thanh_lap"].ToString();
                lbl_csdt_ten.Text = _ds_dn.Tables[0].Rows[0]["ten_co_so_dt"].ToString();
                lbl_csdt_website.Text = _ds_dn.Tables[0].Rows[0]["trang_chu"].ToString();
            }
            else
            {
                return false;
            }
        }
        return true;
    }
    protected void btn_dangky_hoc_Click(object sender, EventArgs e)
    {
        int _kq = Convert.ToInt32(Session["so1"].ToString()) + Convert.ToInt32(Session["so2"].ToString());
        if (!_kq.ToString().Trim().Equals(txt_dk_ketqua.Text.Trim()))
        {
            lbl_ketqua.Visible = true;
        }
        else
        {
            lbl_ketqua.Visible = false;
            Session.Remove("so1"); Session.Remove("so2");
            
            SqlParameter[] paramlist = new SqlParameter[8];
            paramlist[0] = new SqlParameter("@id", _common.GenerateID());
            paramlist[1] = new SqlParameter("@ten_day_du", txt_dk_tendaydu.Text);
            paramlist[2] = new SqlParameter("@dia_chi", txt_dk_diachi.Text);
            paramlist[3] = new SqlParameter("@dien_thoai", txt_dk_sodienthoai.Text);
            paramlist[4] = new SqlParameter("@email", txt_dk_email.Text);
            paramlist[5] = new SqlParameter("@ma_khoa_hoc", _id);
            paramlist[6] = new SqlParameter("@ngay_sinh", txt_ngaysinh_RadDateInput.SelectedDate.Value);
            paramlist[7] = new SqlParameter("@gioi_tinh", drop_gioitinh.SelectedIndex == 0 ? "Nữ":"Nam");

            if (_common.ExeceuteByStoreProcedure(paramlist, "HOCVIEN_DANGKY_ADD") > 0)
                Response.Redirect("Default.aspx");
           
        }
    }
    protected void btn_view_result_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Default.aspx?page=kdt_ds_dk&ma=" + _id);
        Response.Redirect(Page.ResolveUrl("~/Default.aspx?page=kdt_ds_dk&ma=" + _id));
    }
}